/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.model.ProdBrowseLog;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.ProdBrowseLogMapper;
import com.yami.shop.service.ProdBrowseLogService;
import com.yami.shop.service.ProductService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 商品浏览记录表
 *
 * @author LGH
 * @date 2021-11-01 10:43:09
 */
@Service
@AllArgsConstructor
public class ProdBrowseLogServiceImpl extends ServiceImpl<ProdBrowseLogMapper, ProdBrowseLog> implements ProdBrowseLogService {

    private final ProdBrowseLogMapper prodBrowseLogMapper;

    private final ProductService productService;

    @Override
    public IPage<ProdBrowseLog> pageByUserId(PageParam<ProductDto> page, String userId) {
        return prodBrowseLogMapper.pageByUserId(page, userId, I18nMessage.getLang());
    }

    @Override
    public void saveInfo(ProdBrowseLog prodBrowseLog) {
        Date now = new Date();
        int count = prodBrowseLogMapper.selectCount(Wrappers.lambdaQuery(ProdBrowseLog.class)
                .eq(ProdBrowseLog::getUserId, prodBrowseLog.getUserId())
                .eq(ProdBrowseLog::getBrowseTime, DateUtil.beginOfDay(now))
                .eq(ProdBrowseLog::getProdId, prodBrowseLog.getProdId())
        );
        if (count == 0) {
            Product product = productService.getProductByProdId(prodBrowseLog.getProdId(), I18nMessage.getDbLang());
            if (Objects.nonNull(product)) {
                prodBrowseLog.setShopId(product.getShopId());
            }
            prodBrowseLog.setCreateTime(now);
            prodBrowseLog.setBrowseTime(DateUtil.beginOfDay(now));
            prodBrowseLogMapper.insert(prodBrowseLog);
        } else if (count == 1){
            ProdBrowseLog dbLog = prodBrowseLogMapper.selectOne(Wrappers.lambdaQuery(ProdBrowseLog.class)
                    .eq(ProdBrowseLog::getUserId, prodBrowseLog.getUserId())
                    .eq(ProdBrowseLog::getBrowseTime, DateUtil.beginOfDay(now))
                    .eq(ProdBrowseLog::getProdId, prodBrowseLog.getProdId()));
            dbLog.setCreateTime(now);
            prodBrowseLogMapper.updateById(dbLog);
        }
    }

    @Override
    public void deleteByIds(List<Long> prodBrowseLogIds, String userId) {
        prodBrowseLogMapper.delete(Wrappers.lambdaQuery(ProdBrowseLog.class)
                .in(ProdBrowseLog::getProdBrowseLogId, prodBrowseLogIds)
                .eq(ProdBrowseLog::getUserId, userId)
        );
    }
}
